﻿/*
 * Dept的数据操作类
 * */
using System;
using System.Collections.Generic;
using System.Text;
using WH.NurseSys.Model;
using System.Data.SqlClient;
using System.Data;

namespace WH.NurseSys.DAL
{
    public class DeptDAL
    {
        public bool Insert(Dept dept)
        {
            string sqlText = "insert into depts (id,name) values (@id,@name)";
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@id",dept.Id),
                new SqlParameter("@name",dept.Name),
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows >= 0)
                return true;
            else
                return false;
        }

        public bool Delete(Guid id)
        {
            string sqlText = "delete from depts where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows >= 0)
                return true;
            else
                return false;
        }

        public List<Dept> GetList()
        {
            string sqlText = "select id,name from depts";
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText);
            List<Dept> deptList = new List<Dept>();
            foreach (DataRow dbRow in dbTable.Rows)
            {
                Dept dept = new Dept();
                dept.Id = (Guid)dbRow["id"];
                dept.Name = dbRow["name"].ToString();
                deptList.Add(dept);
            }
            return deptList;
        }

        public Dept GetById(Guid id)
        {
            string sqlText = "select id,name from depts where id=@id";
            SqlParameter parameters = new SqlParameter("@id", id);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            if (dbTable.Rows.Count <= 0)
                return null;
            else
            {
                DataRow dbRow = dbTable.Rows[0];
                Dept dept = new Dept();
                dept.Id = (Guid)dbRow["id"];
                dept.Name = dbRow["name"].ToString();
                return dept;
            }
        }

        public Dept GetByName(string name)
        {
            string sqlText = "select id,name from depts where name=@name";
            SqlParameter parameters = new SqlParameter("@name", name);
            DataTable dbTable = SqlHelper.ExecuteTable(sqlText, parameters);
            if (dbTable.Rows.Count <= 0)
                return null;
            else
            {
                DataRow dbRow = dbTable.Rows[0];
                Dept dept = new Dept();
                dept.Id = (Guid)dbRow["id"];
                dept.Name = dbRow["name"].ToString();
                return dept;
            }
        }

        public bool Update(Dept dept)
        {
            string sqlText = "update depts set name=@name where id=@id";
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@id",dept.Id),
                new SqlParameter("@name",dept.Name),
            };
            int rows = SqlHelper.ExecuteNoQuery(sqlText, parameters);
            if (rows >= 0)
                return true;
            else
                return false;
        }
    }
}
